package facade;

import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;

import java.util.List;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JList;

import org.jdom.Element;

import template.AbstractSystem;

import utils.Dom;

import view.GoogleMapContainer;

import model.Path;

import controler.ShowDeliveryPathListener;



/*********************************************************************************/
public class DeliverySystem extends AbstractSystem{

	private static final long serialVersionUID = 1L;
	private JPanel leftPanel, rightPanel, bottomPanel;
	private JList deliveriesOrderList;
	private JTextField emplacementTextField;
	private Path pathModel;
	
	public DeliverySystem(){
		setCurrentSystem(this);
		addWindowListener(new CloseSystemListener());
		
		pathModel = new Path();

		leftPanel = createPanelAddress(); 
		rightPanel = createPanelGoogle();
		bottomPanel = createPanelButton();
		
		add(leftPanel);
		add(rightPanel);
		add(bottomPanel);

		setTitle("Delivery System");
		setJMenuBar(createMainMenu());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
		setBackground(Color.WHITE);
		setLayout(null);
		
		int screenWidth = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
		int screenHeight = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
		
		setBounds(screenWidth/2 - 450, screenHeight/2 - 300, 900, 600);
		setResizable(false);
		setVisible(true);	
}	
	
	private JLabel createLabelForm(){
		JLabel label = new JLabel("Current emplacement:");
		label.setFont(new Font("Arial", Font.BOLD, 15));
		label.setBounds(10, 25, 200, 20);
		return label;
	}
	
	private JTextField createEmplacementTextField(){
		emplacementTextField = new JTextField();
		emplacementTextField.setBounds(10, 50, 280, 20);
		return emplacementTextField;
	}
	
	private JPanel createPanelAddress(){
		JPanel addressPanel = new JPanel();
		
		addressPanel.setLayout(null);
		addressPanel.setBounds(0,0,300,500);
		addressPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Adress"),BorderFactory.createEmptyBorder(10,10,10,10)));
		addressPanel.add(createLabelForm());
		addressPanel.add(createEmplacementTextField());
		addressPanel.add(createDeliveriesList());
			
		return addressPanel;
	}
	
	private JPanel createPanelGoogle(){
		JPanel googlePanel = new GoogleMapContainer(pathModel);
		googlePanel.setBounds(300,0,590,500);
		googlePanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("API Google"),BorderFactory.createEmptyBorder(10,20,10,20)));
		return googlePanel;
	}
	
	private JPanel createPanelButton(){
		JPanel buttonPanel = new JPanel();
		
		buttonPanel.setLayout(null);
		buttonPanel.setBounds(0,500,900,100);
		buttonPanel.add(createSubmitButton());
		buttonPanel.add(createAcceptButton());
		
		return buttonPanel;
	}
	
	private JList createDeliveriesList(){
		Vector<String> preList = new Vector<String>();
		Element root = Dom.getXMLFile("bookOfDelivery.xml").getRootElement();
		List<Element> children = root.getChildren();
		Iterator<Element> elementsIt = children.iterator();
		
		while(elementsIt.hasNext()){
			Element element = elementsIt.next();
			preList.add(element.getChildText("date")+" ("+element.getChildText("time")+" )");
		}
		
		deliveriesOrderList = new JList(preList);
		deliveriesOrderList.setSelectedIndex(0);
		deliveriesOrderList.setBounds(10, 90, 280, 400);
		deliveriesOrderList.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Delivery Orders"),BorderFactory.createEmptyBorder(10,10,10,10)));
		return deliveriesOrderList;
	}
	
	private JButton createSubmitButton(){
		JButton submit = new JButton ("Show the path");
	 	submit.setBounds(10, 15, 125, 25);
	 	submit.addActionListener(new ShowDeliveryPathListener(pathModel, deliveriesOrderList, emplacementTextField));
	 	return submit;
	}
	
	public JButton createAcceptButton(){
		JButton cancel = new JButton ("Accept this delivery");
		cancel.setBounds(140,15,155,25);
		//cancel.addActionListener(new ShowDeliveryPathListener());
		return cancel;
	}
	
	private JMenuBar createMainMenu(){	
		return createRootMainMenu("Delivery System");	
	}
}
